package logic

import (
	"context"
	"ewaimai/waimairpc/cache"
	"ewaimai/waimairpc/models"

	"ewaimai/waimairpc/internal/svc"
	"ewaimai/waimairpc/waimairpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdatepasswordLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdatepasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatepasswordLogic {
	return &UpdatepasswordLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdatepasswordLogic) Updatepassword(in *waimairpc.UpdatepasswordRequest) (*waimairpc.UpdatepasswordResponse, error) {
	// todo: add your logic here and delete this line

	var u models.User
	models.DB.Debug().Where("id =?", in.Id).Find(&u)
	get := cache.Rdb.Get(l.ctx, "sendSms"+in.Mobile)
	if get.Val() != in.SendCode {
		logx.Errorf("短信验证码获取失败")
	}
	if u.Password == in.Password {
		logx.Error("两次输入的密码不一致")
	}
	if in.Newpassword == in.Password {
		logx.Error("新旧密码一致")
	}

	models.DB.Updates(&u)
	return &waimairpc.UpdatepasswordResponse{
		Id: int64(u.ID),
	}, nil
}
